Imports System.Data.Odbc
'Imports com.fds.common.services.exception
'Imports com.fds.common.services.logging
Public Class GMRUtility
    Dim sTDUSERID As String

    Dim SchemaNM As String = ConfigurationSettings.AppSettings("Environment")
    Dim strDB As String
    Private objTDCall = New GetData
    ' Dim Enviro As String = objTDCall.GetLogOn(Enviro, USERID)

    Public WriteOnly Property SetUserID()
        Set(ByVal Value)
            sTDUSERID = Value
        End Set
    End Property

    Public ReadOnly Property GetDBName(Optional ByVal blnGMRLockChk As Boolean = False)
        'Added the blnGMRLockChk parameter for the Period Lock process. Tables that must
        'be updated are located in MHPMST and therefore when this function period lock 
        'process in production MHPMST must be returned
        Get
            Return objTDCall.GetDBName(blnGMRLockChk)
        End Get
    End Property

    Public Function GetLastLockedPeriod(ByVal strSysID As String) As DataSet

        strDB = objTDCall.GetDBName
        Dim strSQL = "Select AMC_YEAR, AMC_PERIOD,  USER_ID, CREATE_TS "
        strSQL += "From " & strDB & ".GMR_SYS_LOCKING_V "
        strSQL += "Where SYS_ID = 'GMBL' "
        strSQL += "And CREATE_TS = (Select Max(CREATE_TS) From "
        strSQL += strDB & ".GMR_SYS_LOCKING_V Where SYS_ID='GMBL')"

        Dim Ds As New DataSet
        Dim strTblName As String = "tblProc"
        Dim strErrProcNbr As String = ""
        Dim strError As String = ""
        Dim strAMCYear As String = ""
        Dim strAMCPeriod As String = ""

        Try
            objTDCall.CallTD(SchemaNM, sTDUSERID, strSQL, strTblName, strErrProcNbr, Ds)
            'Return Ds
        Catch ex As OdbcException
            Dim strErrEx As String = ""
            Dim strTDErr As String = ""

            strError = CheckODBCErrs(ex, strTDErr)

            'Dim customException As FDSException = New FDSException(strErrEx, ex)
            'customException.setSeverity(FDSException.CRITICAL)
            strError = ex.Message & vbCrLf & " UserID :" & sTDUSERID & " Offending SQL error " & strTDErr & " : " & vbCrLf & strSQL & vbCrLf
            'FDSLogger.critical(strError, customException)
            'Throw customException
        End Try
        Return Ds
    End Function

    Public Function CheckODBCErrs(ByRef ex As OdbcException, ByRef strTDErr As String)
        Dim j As Integer

        For j = 0 To ex.Errors.Count - 1
            strTDErr = ex.Errors(j).NativeError.ToString()
        Next j

        Select Case strTDErr
            Case "-3004"
                Return "Teradata User Access not authorized. "
            Case "-3003"
                Return "Invalid Teradata User Access. "
            Case "-2803"
                Return "Duplicate key entry on insert. Please report page. "
            Case "0"
                Return "Teradata Timeout has occurred. "
            Case "-3706"
                Return "SQL Syntax Error. Please report page and search criteria used. "
            Case "-3707"
                Return "SQL Syntax Error. Please report page and search criteria used. "
            Case Else
                Return "Teradata Error. Please report page and search criteria used. "
        End Select
    End Function

    Public Sub New()
        strDB = objTDCall.GetDBName()
    End Sub
End Class
